/*
 * The Evade Steering Behaviour
 */
class Evade extends Steering {

    HunterAgent target;

    public Evade(PreyAgent p, HunterAgent h) {
        super(p);
        this.target = h;
    }

    PVector calculateRawForce() {
        // Check that agent's centre is not over target
        if (PVector.dist(agent.position, this.target.position) > target.radius) {
            // Time to target formula for the look-ahead time
            PVector distV = PVector.sub(this.target.position, agent.position);
            float distance = distV.mag();
            float tpred = this.agent.k * distance / this.agent.velocity.mag();
            float tlim = 5;
            float min = min(tpred, tlim);
            PVector qpred = PVector.mult(this.target.velocity, min);
            qpred.add(this.target.position);
            // We have qpred, now we do Flee(qpred)
            PVector evade = PVector.sub(agent.position, qpred);
            evade.normalize();
            evade.mult(agent.maxSpeed);
            evade.sub(agent.velocity);
            return evade;
        } else {
            // If agent's centre is over target stop pursuing, the hunter caught the prey
            return new PVector(0, 0);
        }
    }

    // Draw the target
    void draw() {
        pushStyle();
        fill(204, 153, 0);
        ellipse(target.position.x, target.position.y, target.radius, target.radius);
        popStyle();
    }

    void setTarget(HunterAgent target) {
        this.target = target;
    }

}
